/* 
 * File:   Student.cpp
 * Author: Allison
 * 
 * Created on September 30, 2013, 5:09 PM
 */

#include <iomanip>
#include <sstream>
#include "Student.h"

// constructor method to initialize each instance variable.
Student::Student(string firstName, string lastName, string studentID, bool res, double rate) {
    this->firstName = firstName;
    this->lastName = lastName;
    this->studentID = studentID;
    this->resident = res;
    this->rate = rate;
    this->tuitionAmount = 0.0;
    
    if(this->resident == true)
        creditUpperbound = 7;
    else
        creditUpperbound = 12;
}

Student::~Student() {
}

void Student::setFirstName(string firstName) {
    this->firstName = firstName;
}

string Student::getFirstName() const {
    return firstName;
}

void Student::setLastName(string lastName) {
    this->lastName = lastName;
}

string Student::getLastName() const {
    return lastName;
}

void Student::setStudentID(string studentID) {
    this->studentID = studentID;
}

string Student::getStudentID() const {
    return studentID;
}

void Student::setRate(double rate) {
    this->rate = rate;
}

double Student::getRate() const {
    return rate;
}

void Student::setTuitionAmount(double tuitionAmount) {
    this->tuitionAmount = tuitionAmount;
}

double Student::getTuitionAmount() const {
    return tuitionAmount;
}

int Student::computeCreditNum() {
    int creditNum = 0;
    for (int i = 0; i < courseList.size(); i++) {
        creditNum = creditNum + courseList[i].getSemesterHours();
    }
    return creditNum;
}

void Student::computeTuition() {
    setTuitionAmount(0.0); //Error code
}

int Student::courseExists(string courseName, string days, string time) {
    int index = -1;
    if (courseList.size() > 0) {
        for (int i = 0; i < courseList.size(); i++) {
            Course c = courseList[i];
            if (c.getCourseName() == courseName)
                if (c.getSchedule().getDays() == days)
                    if (c.getSchedule().getTime() == time)
                        index = i;
        }
    }
    return index;
}

bool Student::enrollCourse(Course c) {
    int i = courseExists(c.getCourseName(), c.getSchedule().getDays(), c.getSchedule().getTime());
    if (i==-1) {
        courseList.push_back(c);
        return true;
    }
    else
        return false;
}
bool Student::cancelCourse(Course c) {
    int i = courseExists(c.getCourseName(), c.getSchedule().getDays(), c.getSchedule().getTime());
    if (i == -1)
        return false;
    else {
        courseList.erase(courseList.begin()+i);
        return true;
    }
}

int Student::compareTo(Student other) {
    int result;
    if (lastName == other.lastName)
        result = firstName.compare(other.firstName);
    else
        result = lastName.compare(other.lastName);

    return result;
}

// The toString method returns a string describing
// the value of each instance variable.
string Student::toString() {
    computeTuition();

    return "\nFirst name:\t\t" + firstName + "\n"
            + "Last name:\t\t" + lastName + "\n"
            + "Student ID:\t\t" + studentID + "\n"
            + "Tuition Amount:\t\t" + currencyFormat(tuitionAmount)
            + "\n\n";
}

string Student::currencyFormat(double amount) {
    return static_cast<stringstream*>( &(stringstream() <<
            fixed << setprecision(2) << "$" << amount) )->str();
}
